// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    userData: {
      name: '',
      id: '',
      title: '',
      status: '',
      // 随机数
      randomNum: 4,
      rgb: 'rgb(7,193,96)',
    },
    status: false,
    // 控制取色器显示
    pick: false
  },
  toPick: function () {
    this.setData({
      pick: true
    })
  },

  pickColor(e) {
    let rgb = e.detail.color;
    this.setData({
      'userData.rgb': e.detail.color
    })
  },
  // 输入框事件
  nameInput(e) {
    this.setData({
      'userData.name': e.detail.value
    })
  },
  idInput(e) {
    this.setData({
      'userData.id': e.detail.value
    })
  },
  randomChange(e) {
    this.setData({
      'userData.randomNum': e.detail.value
    })
  }
  ,
  // 进校
  inTo(event) {
    this.setData({
      'userData.title': event.target.dataset.title,
      'userData.status': this.data.status
    })
    this.rememberMe()
    const temp = JSON.stringify(this.data.userData)
    wx.redirectTo({
      url: '/pages/access/access?data=' + temp,
    })
  },
  // 离校
  outTo(event) {
    this.setData({
      'userData.title': event.target.dataset.title,
      'userData.status': this.data.status
    })
    this.rememberMe()
    wx.redirectTo({
      url: '/pages/outTo/outTo?data=' + JSON.stringify(this.data.userData),
    })
  },
  // 勾选框状态改变时
  checkboxChange() {
    this.setData({
      status: !this.data.status
    })
  },
  // 是否记住状态
  rememberMe() {
    if (this.data.status) {
      wx.setStorageSync('name', this.data.userData.name)
      wx.setStorageSync('id', this.data.userData.id + '')
      wx.setStorageSync('isRem', this.data.status)
    } else {
      wx.clearStorageSync()
    }
  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad() {
    const value = wx.getStorageSync('isRem')
    const name = wx.getStorageSync('name')
    const id = wx.getStorageSync('id')
    if (value) {
      this.setData({
        status: value,
        'userData.name': name,
        'userData.id': id
      })
    }
  },
  /**
  * 用户点击右上角分享
  */
  onShareAppMessage() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  }
})
